#!/bin/bash
##pull code

function initialize {
    DEFAULT_VERSION=`date +%Y%m%d`
    # Check if the 'VERSION' parameter is provided
    if [ -z "$VERSION" ]; then
        # If not provided, use the default value
        VERSION="$DEFAULT_VERSION"
    fi
    WORKSPACE_FOLDER=/home/ubuntu/workspace
    LOG_FOLDER=/home/robot/reports
    VOLUMED_FOLDER=/home/robot/space
    VOLUMED_TANGO_LIB_FOLDER=$VOLUMED_PATH/TANGOLIB
	TANGO_LIB_FOLDER=$WORKSPACE_FOLDER/tango/lib
    LOCAL_LOG_FOLDER=/home/ubuntu/workspace/logs/Space
    CURRENT_LOG_FOLDER=$LOG_FOLDER/$VERSION
    LOCAL_CASE_FOLDER=$WORKSPACE_FOLDER/space
    EXECUTION_FOLDER=$VOLUMED_FOLDER/Cases
    ALLURE_FOLDER=$LOCAL_LOG_FOLDER/$VERSION/allure_log
    POD=SPACE-$RANDOM
    rm -rf $CURRENT_LOG_FOLDER
    echo "Current Version is $VERSION"
}

function pullCode {
    echo "git@git.signintra.com:test-automation/space.git"
	echo "Update space"
	su ubuntu -c 'ssh -T git@git.signintra.com'
	cd $WORKSPACE_FOLDER/space
	su ubuntu -c 'git fetch --all;
    git reset --hard origin/master;
    git clean -df;'
	su ubuntu -c "chmod +x -R $WORKSPACE_FOLDER/space/Execution"
}
##run 

function startContainer {
    podman run --name rf-$POD \
    --pod new:$POD -dt -P \
    -v $LOCAL_CASE_FOLDER:$VOLUMED_FOLDER \
	-v $TANGO_LIB_FOLDER:$VOLUMED_TANGO_LIB_FOLDER \
    -v $LOCAL_LOG_FOLDER:$LOG_FOLDER \
    docker.signintra.com/test-automation/space/rf:latest
    echo "VNC Ports for Robot Container"
    podman port rf-$POD
}

function execution {
    RUN_ARG1="--skip BLOCK --listener $VOLUMED_TANGO_LIB_FOLDER/tango_robotframework\;$CURRENT_LOG_FOLDER/allure_log"
    RUN_ARG2="--removekeywords PASSED --maxerrorlines 10 --variablefile $VOLUMED_FOLDER/Configure/LOCAL.py"
    RUN_ARG3="-d $CURRENT_LOG_FOLDER" 
    RUN_ARG5="-R output1.xml -o output2.xml" 
    podman exec -it -w $CURRENT_LOG_FOLDER rf-$POD /bin/bash -c \
	" 
        cd $VOLUMED_FOLDER
        robot $RUN_ARG1 $RUN_ARG2 $RUN_ARG3 -o output1.xml $EXECUTION_FOLDER
        robot $RUN_ARG1 $RUN_ARG2 $RUN_ARG3 -R $CURRENT_LOG_FOLDER/output1.xml -o output2.xml $EXECUTION_FOLDER
        rebot $RUN_ARG3 -r ReportLevel2.html -l LogLevel2.html --suitestatlevel 2 -o output.xml --merge $CURRENT_LOG_FOLDER/*xml
    "    
}

function cleanup {
	podman pod rm -f $POD
	podman volume prune --force
}

function createAllureEnvFile {
	# Add environment & executor information in allure log
    mkdir -p $ALLURE_FOLDER
    sudo chown -R ubuntu:ubuntu $LOCAL_LOG_FOLDER
	sudo chmod 755 -R $LOCAL_LOG_FOLDER
	echo -e "allureVersion=`allure --version`\nProject=Space\nVersion=$VERSION" > $ALLURE_FOLDER/environment.properties
	echo -e "{\n    \"type\":\"manual build\",\n    \"buildName\":\"Space_deploy#1\",\n    \"reportName\":\"space\"\n}" > $ALLURE_FOLDER/executor.json
}

function generateAllureReport {
    ##1.add verison record
    addVerisonRecord
    ##2.generate allure report
    echo "Allure report start :$(date +%Y-%m-%d\ %H:%M:%S)"
    allure generate --clean $ALLURE_FOLDER -o $LOCAL_LOG_FOLDER/Allure_Report_$VERSION
    echo "Allure report end :$(date +%Y-%m-%d\ %H:%M:%S)"
    sudo chmod 755 -R $LOCAL_LOG_FOLDER
    #Remove behaviors widget on overview page
    sed -i "187,192 d" $LOCAL_LOG_FOLDER/Allure_Report_$VERSION/plugins/behaviors/index.js
    ##3.add allure in history
    cd $WORKSPACE_FOLDER/space/Execution
    python3 -c "import AllureUtils;AllureUtils.add_allure_in_history(\"$VERSION\")"
}

function addVerisonRecord {
    ###generate allure report and upload result set
    cd $WORKSPACE_FOLDER/space/Execution
    python3 -c "import AllureUtils;AllureUtils.copy_history(\"$VERSION\")"
    #git push DailyJobVersionRecord.json
    commitVersionRecord
}

function commitVersionRecord {
    cd  $WORKSPACE_FOLDER/space/Execution
    echo "Update DailyJobVersionRecord.json"
    su ubuntu -c 'ssh -T git@git.signintra.com'
    su ubuntu -c 'git pull origin main'
    su ubuntu -c 'git add DailyJobVersionRecord.json'
	su ubuntu -c 'git commit -m /"Update DailyJobVersionRecord file/"'
    su ubuntu -c 'git pull origin main'
    su ubuntu -c 'git push -u origin main'
}

function importResult {
    cd $WORKSPACE_FOLDER/space/lib/testRail
    python3 -c "import importResult;importResult.import_result_toTestRail(\"$VERSION\",\"$LOCAL_LOG_FOLDER/$VERSION/output.xml\")"
}

VERSION=$1
initialize
pullCode
createAllureEnvFile
startContainer
execution
cleanup
generateAllureReport
importResult